//
//  MissionWebMask.m
//  HCNewGoldFinger
//
//  Created by 刘琛 on 2018/11/28.
//  Copyright © 2018年 zjhcsoftios. All rights reserved.
//

#import "MissionWebMask.h"
//#import "WebViewJavascriptBridge.h"
#import "SVProgressHUD.h"
#import "NetworkAddress.h"
#import "ISeeUserInfoModel.h"
#import <MJExtension/MJExtension.h>


#define iSEESCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define iSEESCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface MissionWebMask()<UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *web;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *webHeight;

@property (nonatomic, assign) BOOL isReceipt;

@property (nonatomic, strong) NSString *url;

@property (nonatomic, copy) MissionWebMaskSuccseeBlock success;

@property (nonatomic, copy) MissionWebMaskFailureBlock fail;

//@property WebViewJavascriptBridge* bridge;

@property (nonatomic, strong) NSString *detailIds;

@property (nonatomic, strong) NSString *phoneNum;

@end


@implementation MissionWebMask

- (void)awakeFromNib {
    [super awakeFromNib];
    
    
    _web.delegate = self;
    
    _web.scrollView.bounces=NO;
    
    [_web setOpaque:NO];
    
//    [self createWebBridge];
    
    _web.backgroundColor =[UIColor clearColor];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [self addGestureRecognizer:tap];
}

//- (void)createWebBridge
//{
//
//    [WebViewJavascriptBridge enableLogging];
//
//    _bridge = [WebViewJavascriptBridge bridgeForWebView:_web webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
//        NSLog(@"ObjC received message from JS: %@", data);
//        responseCallback(@"Response for message from ObjC");
//    }];
//
//    //回调成功
//    [_bridge registerHandler:@"getAppointDataSuccess" handler:^(id data, WVJBResponseCallback responseCallback) {
//        NSLog(@"testObjcCallback called: %@", data);
//
//        if (self.success) {
//
//            self.success(@"");
//            [self dismiss];
//        }
//    }];
//
//
//    //回掉失败
//    [_bridge registerHandler:@"getAppointDataFail" handler:^(id data, WVJBResponseCallback responseCallback) {
//        NSLog(@"testObjcCallback called: %@", data);
//        NSString *datastr =data;
//        [SVProgressHUD showErrorWithStatus:datastr];
//    }];
//
//    //回调取消
//    [_bridge registerHandler:@"cancelGetAppointData" handler:^(id data, WVJBResponseCallback responseCallback) {
//        NSLog(@"testObjcCallback called: %@", data);
//
//        [self dismiss];
//    }];
//}

- (void)setIsReceipt:(BOOL)isReceipt {
    _isReceipt = isReceipt;
    
    if (_isReceipt) {
        _webHeight.constant = 260;
    }else {
        _webHeight.constant = 440;
    }
}

+ (void)showBusinessNumberProcessView:(UIView *)view isReceipt:(BOOL)isReceipt url:(NSString *)url success:(MissionWebMaskSuccseeBlock)success failure:(MissionWebMaskFailureBlock)fail{
    
    MissionWebMask *mask = [[NSBundle mainBundle] loadNibNamed:@"MissionWebMask" owner:self options:nil].lastObject;
    mask.frame = CGRectMake(0, 0, iSEESCREEN_WIDTH, iSEESCREEN_HEIGHT);
    mask.isReceipt = isReceipt;
    mask.url = url;
    mask.success = success;
    mask.fail = fail;
    [view addSubview:mask];
    
    [mask show];
}


- (void)show {
    
    [_web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_url]]];

}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"%@", request.URL.absoluteString);
    return YES;
}

- (void)dismiss {
    
    [self removeFromSuperview];
}

#pragma mark 回单

+ (void)showReceiptView:(UIView *)view isReceipt:(BOOL)isReceipt detailIds:(NSString *)detailIds phoneNum:(NSString *)phoneNum url:(NSString *)url success:(MissionWebMaskSuccseeBlock)success failure:(MissionWebMaskFailureBlock)fail {
    MissionWebMask *mask = [[NSBundle mainBundle] loadNibNamed:@"MissionWebMask" owner:self options:nil].lastObject;
    mask.frame = CGRectMake(0, 0, iSEESCREEN_WIDTH, iSEESCREEN_HEIGHT);
    mask.success = success;
    mask.fail = fail;
    mask.detailIds = detailIds;
    mask.phoneNum = phoneNum;
    mask.isReceipt = isReceipt;
    mask.url = [NSString stringWithFormat:@"%@newmarketapp/#/receipt-dialog?pdata=%@", NMarketMainUrl, [[mask packagePdata] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    [view addSubview:mask];
    [mask show];
}

//拼接Pdata数据
- (NSString *)packagePdata
{

    NSString *seccion = [[NSUserDefaults standardUserDefaults] objectForKey:NMarketUserSession];
    
    ISeeUserInfoModel *userInfo = [ISeeUserInfoModel mj_objectWithKeyValues:[[[NSUserDefaults standardUserDefaults] objectForKey:NMarketUserInfo] mj_JSONObject]];
    
    NSMutableDictionary *headerDic = [NSMutableDictionary dictionary];
    [headerDic setValue:seccion forKey:@"session"];
    //    [headerDic setValue:self.devicetoken forKey:HCGFReqDeviceToken];
    [headerDic setValue:[[[NSBundle mainBundle] infoDictionary]objectForKey:@"CFBundleShortVersionString"] forKey:@"version"];
    [headerDic setValue:@"IOS" forKey:@"platform"];
    [headerDic setValue:[NSString stringWithFormat:@"%@"
                         ,userInfo.userId] forKey:@"userId"];
    [headerDic setValue:[[UIDevice currentDevice] systemVersion] forKey:@"platformVersion"];
    
    
    NSMutableDictionary *body = [[NSMutableDictionary alloc] init];
    if (self.detailIds) {
        [body setValue:self.detailIds forKey:@"orderIds"];
    }
    if (self.phoneNum) {
        [body setObject:self.phoneNum forKey:@"ywhm"];
    }
    
    NSDictionary *package = [NSDictionary dictionaryWithObjectsAndKeys:headerDic, @"header", body,@"body",nil];
    
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:package options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *nsjson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    return nsjson;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
